package kuwo

import (
	"api-go/serializer"
	"api-go/util"
	"net/url"
)

// 搜索所需信息
type Search struct {
	Key    string `form:"key"`
	Limit  string `form:"limit"`
	Offset string `form:"offset"`
	Type   string `form:"type"`
}

//获取搜索信息
func (service *Search) SearchInfo() *serializer.Response {

	if val := service.Key; val != "" {
		service.Key = val
	} else {
		service.Key = "周杰伦"
	}

	if val := service.Limit; val != "" {
		service.Limit = val
	} else {
		service.Limit = "30"
	}

	if val := service.Offset; val != "" {
		service.Offset = val
	} else {
		service.Offset = "1"
	}

	// 关键字搜索（searchMusicBykeyWord: 单曲, searchAlbumBykeyWord: 专辑, searchArtistBykeyWord: 歌手, searchPlayListBykeyWord: 歌单, searchMvBykeyWord: MV）
	if val := service.Type; val != "" {
		service.Type = val
	} else {
		service.Type = "1"
	}
	//1: 单曲, 2: 专辑, 3: 歌手, 5: MV, 9：歌单
	switch service.Type {
	case "1":
		service.Type = "searchMusicBykeyWord"
	case "2":
		service.Type = "searchAlbumBykeyWord"
	case "3":
		service.Type = "searchArtistBykeyWord"
	case "5":
		service.Type = "searchMvBykeyWord"
	case "9":
		service.Type = "searchPlayListBykeyWord"
	}

	//此处需要对中文进行转码，否则会无法请求第三方api
	service.Key = url.QueryEscape(service.Key)
	url := "https://kuwo.cn/api/www/search/" + service.Type + "?key=" + service.Key + "&rn=" + service.Limit + "&pn=" + service.Offset + "&httpsStatus=1"

	// fmt.Println(url)
	res, err := util.KuwoGet(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}

//获取热搜
func (service *Search) SearchHot() *serializer.Response {

	url := "http://m.kuwo.cn/newh5app/api/mobile/v1/search/hotword"

	res, err := util.Get(url)
	if err != nil {
		return err
	}
	return serializer.OkResponse(res)
}
